package com.trust.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.trust.agri.util.BeanUtil;
import com.trust.entity.Medicine;
import com.trust.entity.MedicineStandard;
import com.trust.entity.Param.MedicineListParam;
import com.trust.entity.Param.SupplyListParam;
import com.trust.entity.Supply;
import com.trust.entity.VO.MedicineListVO;
import com.trust.entity.VO.MedicineVO;
import com.trust.entity.VO.SupplyVO;
import com.trust.entity.result.DropDownResult;
import com.trust.mapper.MedicineMapper;
import com.trust.service.DictionaryService;
import com.trust.service.MedicineService;
import com.trust.service.MedicineStandardService;
import com.trust.service.SystemCityService;
import com.trust.util.NullCheck;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Slf4j
@Service
public class MedicineServiceImpl extends ServiceImpl<MedicineMapper, Medicine> implements MedicineService {


    @Resource
    private DictionaryService dictionaryService;

    @Resource
    private SystemCityService systemCityService;

    @Resource
    private MedicineStandardService medicineStandardService;

    @Override
    public String getNameById(Integer id) {
        Medicine medicine = this.getById(id);
        if (null == medicine){
            return "";
        }
        return medicine.getName();
    }


    @Override
    public Page<MedicineListVO> list(MedicineListParam param) {
        LambdaQueryWrapper<Medicine> wrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotBlank(param.getName())){
            wrapper.like(Medicine::getName, param.getName());
        }
        if (StringUtils.isNotBlank(param.getCode())){
            wrapper.like(Medicine::getCode, param.getCode());
        }
        wrapper.orderByDesc(Medicine::getId);
        Page<Medicine> page = this.page(new Page<>(param.getPageNum(), param.getPageSize()), wrapper);
        Page<MedicineListVO> targetPage = BeanUtil.sourcePage2targetPage(page, MedicineListVO.class);
        for (MedicineListVO record : targetPage.getRecords()) {
            record.setGradeName(null == record.getGrade() ? null : dictionaryService.getNameById(record.getGrade()));
        }
        return targetPage;
    }

    @Override
    public List<DropDownResult> dropDown() {
        LambdaQueryWrapper<Medicine> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Medicine::getEnabled, 1);
        wrapper.orderByDesc(Medicine::getId);
        List<Medicine> list = this.list(wrapper);
        return BeanUtil.sourceList2targetList(list, DropDownResult.class);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Medicine saveAndUpdate(MedicineVO param) {
        Medicine medicine = BeanUtil.source2target(param, new Medicine());
        //种苗单位
        if (null == param.getSeedUnit() && StringUtils.isNotBlank(param.getSeedUnitName())){
            medicine.setSeedUnit(dictionaryService.save(60, param.getSeedUnitName()));
        }
        //药材等级
        if (null == param.getGrade() && StringUtils.isNotBlank(param.getGradeName())){
            medicine.setGrade(dictionaryService.save(62, param.getGradeName()));
        }
        if (param.getIsDefault() > 0){
            LambdaQueryWrapper<Medicine> wrapper = new LambdaQueryWrapper<>();
            wrapper.gt(Medicine::getIsDefault, 0);
            Medicine one = this.getOne(wrapper);
            if (null != one){
                one.setIsDefault(0);
                this.updateById(one);
            }
        }
        this.saveOrUpdate(medicine);
        medicineStandardService.removeByMedicineId(medicine.getId());
        for (MedicineStandard standard : param.getStandardList()) {
            NullCheck.isValid(standard);
            standard.setMedicineId(medicine.getId());
            medicineStandardService.save(standard);
        }
        return medicine;
    }


}
